<template>
  <!-- 基础材质，可以用它赋予几何体一种简单的颜色，或者显示几何体的线框 -->
  <div class="mesh-basic">
    <div id="scene"></div>
    <div class="controls-box">
      <section>
        <el-row>
          <el-col :span="8" class="label-col"><label>mesh</label></el-col>
          <el-col :span="16">
            <el-select v-model="properties.selectMesh" placeholder="请选择">
              <el-option
                v-for="item in selectMeshOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-checkbox v-model="properties.visible">是否可见</el-checkbox>
        </el-row>
        <el-row>
          <el-col :span="8" class="label-col"><label> 材质颜色</label></el-col>
          <el-col :span="16">
            <div class="color-select-layer">
              <el-input :value="properties.color"></el-input>
              <div>
                <el-color-picker v-model="properties.color"></el-color-picker>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-checkbox v-model="properties.transparent">是否透明</el-checkbox>
        </el-row>
        <el-row>
          <el-checkbox v-model="properties.wireframe"
            >是否渲染为线框</el-checkbox
          >
        </el-row>
        <el-row v-if="properties.transparent">
          <el-col :span="8">
            <span class="vertice-span">{{ properties.opacity.name }}</span>
          </el-col>
          <el-col :span="13">
            <el-slider
              v-model="properties.opacity.value"
              :min="properties.opacity.min"
              :max="properties.opacity.max"
              :step="properties.opacity.step"
            ></el-slider>
          </el-col>
          <el-col :span="3">
            <span class="vertice-span">{{ properties.opacity.value }}</span>
          </el-col>
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

let scene; //场景
let camera; //相机
let renderer; //渲染器
let controls; //控制器
export default {
  name: "demo",
  data() {
    return {
      selectMeshOptions: [
        {
          value: "cube",
          label: "cube",
        },
        {
          value: "sphere",
          label: "sphere",
        },
        {
          value: "plane",
          label: "plane",
        },
      ],
      properties: {
        opacity: {
          name: "opacity",
          value: 0.3,
          min: 0,
          max: 1,
          step: 0.1,
        },
        selectMesh: "cube",
        transparent: false,
        wireframe: true,
        visible: true,
        color: "#ccffcc",
      },
      cube: null,
      sphere: null,
      plane: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //初始化
    init() {
      this.createScene();
      this.createMesh();
      this.createCubeAndSphere();
      this.createLight();
      this.createCamera();
      this.createRender();
      this.createControls();
      this.render();
    },
    //创建场景
    createScene() {
      scene = new THREE.Scene();
    },
    //创建网格模型
    createMesh() {
      const planeGeometry = new THREE.PlaneGeometry(100, 100, 4, 4); //创建一个平面对象
      const planeMaterial = new THREE.MeshLambertMaterial({
        color: 0x777777,
      });
      const plane = new THREE.Mesh(planeGeometry, planeMaterial);
      plane.receiveShadow = true;

      //设置平面位置
      plane.rotation.x = -0.5 * Math.PI;
      plane.position.set(0, -20, 0);

      scene.add(plane);
    },
    //创建正方体、球、平面
    createCubeAndSphere() {
      const sphereGeometry = new THREE.SphereGeometry(14, 20, 20);
      const cubeGeometry = new THREE.BoxGeometry(15, 15, 15);
      const planeGeometry = new THREE.PlaneGeometry(14, 14, 4, 4);

      this.meshMaterial = new THREE.MeshBasicMaterial({ color: 0x7777ff });

      this.sphere = new THREE.Mesh(sphereGeometry, this.meshMaterial);
      this.cube = new THREE.Mesh(cubeGeometry, this.meshMaterial);
      this.plane = new THREE.Mesh(planeGeometry, this.meshMaterial);

      this.sphere.position.set(-12, 3, 2);
      this.cube.position = this.sphere.position;
      this.plane.position = this.sphere.position;

      scene.add(this.cube);
    },
    //创建光源
    createLight() {
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); //创建环境光
      scene.add(ambientLight);

      const spotLight = new THREE.SpotLight(0xffffff); //创建聚光灯
      spotLight.position.set(-40, 60, -10);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; //窗口宽高比

      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(-80, 60, 40);

      camera.lookAt(new THREE.Vector3(10, 0, 0)); //设置相机方向
      scene.add(camera);
    },
    //创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer();
      renderer.setSize(element.clientWidth, element.clientHeight); //设置渲染区域尺寸
      renderer.shadowMap.enabled = true; //显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); //设置背景颜色
      element.appendChild(renderer.domElement);
    },
    updateMesh(selectMesh) {
      scene.remove(this.sphere);
      scene.remove(this.cube);
      scene.remove(this.plane);

      switch (selectMesh) {
        case "sphere":
          scene.add(this.sphere);
          break;
        case "cube":
          scene.add(this.cube);
          break;
        case "plane":
          scene.add(this.plane);
          break;
      }
    },
    updateFun() {
      // 材质颜色
      this.meshMaterial.color.setStyle(this.properties.color);
      // 是否透明
      this.meshMaterial.transparent = this.properties.transparent;
      // 透明度：定义物体有多透明。与属性 transparent 一起使用才有效。取值范围是 0~1
      this.meshMaterial.opacity = this.properties.opacity.value;
      // 线框： 当此属性设为 true 时，物体将显示为线框。
      this.meshMaterial.wireframe = this.properties.wireframe;
      //是否可见：定义此材质是否可见。设为 false 时，那么在场景中就看不到该物体
      this.meshMaterial.visible = this.properties.visible;

      this.updateMesh(this.properties.selectMesh);
    },
    render() {
      this.updateFun();
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    //创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
      controls.target.copy(this.plane.position);
    },
  },
};
</script> 

<style scoped>
.mesh-basic {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.controls-box {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
.label-col {
  padding: 8px 5px;
}
.color-select-layer {
  display: flex;
}
.vertice-span {
  line-height: 38px;
  padding: 0 2px 0 10px;
}
</style>
